package com.cnoja.jmsncn.utils.command;

import org.apache.log4j.Logger;

import com.cnoja.jmsncn.command.DefaultResponse;
import com.cnoja.jmsncn.inf.IResponse;
import com.cnoja.jmsncn.kernel.exceptions.CommandValidateException;
import com.cnoja.jmsncn.utils.text.StringUtils;

public class CommandUtils {

	private static Logger logger = Logger.getLogger(CommandUtils.class);
	
	private static Long transactionId=0L;

	public static IResponse extractResponse(String commandString) {
		if (StringUtils.isNullOrEmpty(commandString)) {
			return null;
		}
		String[] split = commandString.split("\\s");
		if (split.length > 1) {
			IResponse response = new DefaultResponse(Long.valueOf(split[1]),
					commandString);
			return response;
		} else {
			throw new IllegalArgumentException(
					"The string must contain 2 groups of word or above");
		}

	}
	
	
	public static String[] split(String command)throws CommandValidateException{
		if (StringUtils.isNullOrEmpty(command)) {
			throw new CommandValidateException(
					"The response content is null or empty!");
		}
		return command.split("\\s");
	}
	
	public synchronized static Long nextId(){
		return transactionId++;
	}
	
	public static String extractHost(String connectString){
		return connectString.split(":")[0].trim();
	}
	public static int extractPort(String connectString){
		return Integer.valueOf(connectString.split(":")[1].trim());
	}

}
